<?php
/*
 * Copyright (c) Taras Kudla, 2010
 */
class user extends kernel
	{
	private $UserTable      = "users";	
	private $CookieLifeTime = 36000;
	private $inAlready      = false;
	private $userAccess 	= array(-1);
	private $userID     	= null;
	private $userGroup	= 0;
	private $UserName	="guest";
	
	public function init(){
		if($this->core->get("runmode")==_CONSOLE_RUNMODE)
			return null;			
		$this->processUserStart();		
	}

	private function processUserStart(){
		if(!$this->core->session->exists("UserSys")){	
			$this->core->session->UserSys=rand(10,100);
			}
		if(!$this->core->session->exists("UserGroup"))
			{
			$this->SetupGuest();		
			}
		else {$this->LaunchAccess($this->core->session->get("UserGroup"));  }
		$this->userID       = $this->core->session->get("UserID");
		$this->inAlready    = $this->IsInSystem();
		$this->UserName     = ($this->core->session->exists("UserName"))?$this->core->session->get("UserName"):null;
		$this->userGroup    = ($this->core->session->exists("UserGroup"))?$this->core->session->get("UserGroup"):$this->userGroup;
		$this->LoggedIn();
		}
	
	private function LoggedIn(){
			
		if((int)$this->GetUserID()!=0 && $this->CheckUser()){
			$this->core->dbo->update("users_session"," set user_id='".$this->GetUserID()."' where session_id='".$this->core->session->GetSid()."'");
			}
	}
	
	public function IsInSystem(){
		if(	isset($_COOKIE[$this->core->session->GetSessionName()]) 
			&& $this->core->session->exists("UserGroup") 
			&& $this->core->session->exists("UserID")
			&& $this->core->session->exists("UserName")
			&& $this->core->session->get("UserID")!=0		
			)
			{
			return true;
			}
		else
			{	
			return false;		
			}	
		}
		
	public function SysExit(){
			$this->core->session->delete("UserHash");
			$this->core->session->delete("UserName");
			$this->core->session->delete("UserID");
			$this->core->session->delete("UserGroup");
			$this->core->session->destroymysession();
			return true;	
		}
			
	public function SysAuth($email,$passwd,$remember=false,$header=false){
		$sql=" where email='".$this->core->escapeString($email)."' and passwd='".$this->core->encodeString($passwd)."' limit 1 offset 0";
		$Query=$this->core->dbo->select($this->UserTable,"id,firstname,lastname,hash,changemod,changeaccess,groupid",$sql,_RESULT_ROW);
		if(count($Query)!=0 && !$this->inAlready){
			$this->core->dbo->update("users","  set  lastip ='".$this->core->escapeString( isset($_SERVER["REMOTE_ADDR"])?$_SERVER["REMOTE_ADDR"]:"0.0.0.0" )."',  lastdate ='".date("Y-m-d H:i:s")."' where  id ='".$Query["id"]."'");
			$this->core->session->set("UserName",$Query["firstname"]." ".$Query["lastname"]);		
			$this->core->session->set("UserID",$Query["id"]);
			$this->core->session->set("UserGroup",$Query["groupid"]);
            $this->UserName = $Query["firstname"]." ".$Query["lastname"];
            $this->userID   = $Query["id"];
			if($remember){
				$this->inAlready=true;
				$this->core->session->ResetSessionLifetime(2678400); // Remember for a month
			}
			$this->core->log->write("user $email entered".($remember?" with remembering":null),_LOG_MESSAGE,_AUTH_LOG);
			if( $header)
				$this->core->base->redirect();
			return true;
			}else
				return false;
		}	
		
	private function SetupGuest(){
		$this->core->DEBUG("USER","Setup-Guest Function");
		if( !$this->core->session->exists("UserGroup")){
								
			$this->GetGuestFunctions();
			$this->core->session->set("UserID",0); 
			if($this->core->session->exists("UserHash"))$this->core->session->delete("UserHash");
			if($this->core->session->exists("UserName"))$this->core->session->delete("UserName");
		}		
		return true;
	}
		
	private function GetGuestFunctions(){
		$this->userAccess=array_merge($this->userAccess,$this->core->dbo->select("functions","hash"," where  guest ='1'",_RESULT_LIST));
	}
	/*
	 * TODO:
	 * users accessing functions
	 */
	private function LaunchAccess($group){
		
		$time = microtime(1);
		$Query=$this->core->dbo->select("groups","access"," where  id ='".$this->core->escapeString($group)."' limit 1 offset 0",_RESULT_ROW);
		if(count($Query)!=1)return false;
		$this->userAccess=array_merge($this->userAccess,explode(",",$Query["access"]));
		$this->GetGuestFunctions();
		$this->core->profiler("user","user access settings loaded",$time);
		return true;
	}
	
	public function resetaccess(){
		$group = $this->GetUserGroup();
		if( $group==0)
			return false;
		$time = microtime(1);
		$Query=$this->core->dbo->select("groups","access"," where  id ='".$this->core->escapeString($group)."' limit 1 offset 0",_RESULT_ROW);
		if(count($Query)!=1)return false;
		$this->userAccess=array_merge($this->userAccess,explode(",",$Query["access"]));
		$this->GetGuestFunctions();
		$this->core->profiler("user","user access settings loaded",$time);
		return true;		
	}
	


	 public function GetUserID()
	 	{
	 	
	 	return $this->userID;
	 	}
	
	public function GetUserGroup(){
		
		return $this->userGroup;
	}
	
	public function GetMyIP(){
		return $_SERVER["REMOTE_ADDR"];
	}
	
	public function GetUserFingerprint(){
		
		return $this->userID."@".$this->GetUserName();
	}	
	
	public function  GetUserAccess(){
		return $this->userAccess;
		}
	
	public function checkaccess($hash){
		return (bool)in_array($hash,$this->userAccess);
	}
		
	public function GetChangeAccess()
		{
		
		return $this->changeAccess;
		}
	public function GetUserIP(){
		return $_SERVER["REMOTE_ADDR"];
	}
		
	public function GetChangeMod()
		{
		
		return $this->changeMod;
		}

	public function GetUserName()
		{
			return ($this->UserName!=null)?$this->UserName:"guest";
		}

	public function CheckUser()
		{
			return $this->inAlready;
		}
}
?>
